﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CamShared;

namespace CamServer
{
    public partial class ChangeSpeed : DavuxLib.Controls.AeroForm
    {
        ICamera camera;
        INetworkCSCamera ncamera;

        int oldDelay = 0;
        int oldQuality = 0;

        public ChangeSpeed(ICamera c)
        {
            camera = c;
            ncamera = camera as INetworkCSCamera;
            InitializeComponent();

            if (ncamera == null)
            {
                MessageBox.Show("This Camera does not support Quality and FrameRate adjustments.","CamServer Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                oldDelay = ncamera.Delay;
                sQuality.Value = oldQuality = ncamera.Quality;

                if (ncamera.Delay < 20)
                {
                    sFrameRate.Value = 4;
                }
                else if (ncamera.Delay < 50)
                {
                    sFrameRate.Value = 3;
                }
                else if (ncamera.Delay < 250)
                {
                    sFrameRate.Value = 2;
                }
                else if (ncamera.Delay < 1000)
                {
                    sFrameRate.Value = 1;
                }
            }

            _GlassArea.Bottom = ClientRectangle.Height - btnApply.Location.Y + 2;
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Invalidate();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (ncamera != null)
            {
                ncamera.Quality = oldQuality;
                ncamera.Delay = oldDelay;
            }
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void sFrameRate_Scroll(object sender, EventArgs e)
        {
            switch (sFrameRate.Value)
            {
                case 4:
                    ncamera.Delay = 5;
                    break;
                case 3:
                    ncamera.Delay = 50;
                    break;
                case 2:
                    ncamera.Delay = 25;
                    break;
                case 1:
                    ncamera.Delay = 1000;
                    break;
            }
        }

        private void sQuality_Scroll(object sender, EventArgs e)
        {
            if (ncamera != null)
            {
                ncamera.Quality = sQuality.Value;
            }
        }

    }
}
